package com.xuecheng.media.controller;

import com.xuecheng.api.media.MediaApi;
import com.xuecheng.api.media.model.dto.MediaDTO;
import com.xuecheng.api.media.model.qo.QueryMediaModel;
import com.xuecheng.api.media.model.vo.MediaVO;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.util.SecurityUtil;
import com.xuecheng.media.convert.MediaConvert;
import com.xuecheng.media.service.MediaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 媒资信息 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
public class MediaController implements MediaApi {

    @Autowired
    private MediaService  mediaService;


    @PostMapping("media")
    public MediaDTO createMedia(@RequestBody MediaVO vo) {

        //1.获得机构的id值
        Long companyId = SecurityUtil.getCompanyId();

        //2.将vo数据转换为dto数据
        MediaDTO dto = MediaConvert.INSTANCE.vo2dto(vo);
        dto.setCompanyId(companyId);

        //3.调用service获得结果数据
        MediaDTO result = mediaService.createMedia(dto);

        return result;
    }

    @Override
    @PostMapping("media/list")
    public PageVO<MediaDTO> queryMediaList(PageRequestParams params, @RequestBody QueryMediaModel model) {
        Long companyId = SecurityUtil.getCompanyId();

        PageVO pageVO = mediaService.queryMediaList(params, model, companyId);

        return pageVO;
    }

    @Override
    @GetMapping("/media/preview/{mediaId}")
    public String previewMedia(@PathVariable Long mediaId) {
        Long companyId = SecurityUtil.getCompanyId();

        String vodUrl = mediaService.getVODUrl(mediaId,companyId);

        return vodUrl;
    }

    @Override
    @DeleteMapping("media/{mediaId}")
    public void removeMedia(@PathVariable Long mediaId) {
        // 1.获得公司Id
        Long companyId = SecurityUtil.getCompanyId();
        //2,删除
        mediaService.removeMedia(mediaId,companyId);
    }
}
